/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 */
package seedpod.webapp.view.seedpodwidgets;

import seedpod.model.AVPair;
import seedpod.model.ProtegeValueType;

public class LimsWidgetFactory {

	// TODO This factory should get the widget types from a project property file which includes a list
	// of available widgets & their implementation class

	public static enum WidgetType {
		DATE,
		NUMERIC,
		OBJECT_LINK,
		RADIO,
		STRING,
		TEXT,
		TEXTAREA,
		CHECKBOX,
		SELECT,
		COMBO,
		SPREADSHEET,
		FILE,
		PASSWORD,
		BOOLEAN,
		NSELECT
	}

	public static LimsWidget getEditWidget( AVPair avpair ) {
		return getWidget( avpair.getAttribute().getFormWidget(), avpair );
	}

	public static LimsWidget getViewWidget( AVPair avpair ) {
		return getWidget( avpair.getAttribute().getViewWidget(), avpair );
	}

	public static LimsWidget getWidet( WidgetType widgetType ) {
		LimsWidget widget;

		switch (widgetType) {
			case DATE:
				widget = new Date();
				break;

			case NUMERIC:
				widget = new Numeric();
				break;

			case OBJECT_LINK:
				widget = new Object_Link();
				break;

			case TEXT:
				widget = new Text();
				break;

			case STRING:
				widget = new LimsWidget();
				break;

			case RADIO:
				widget = new Radio();
				break;

			case TEXTAREA:
				widget = new TextArea();
				break;

			case CHECKBOX:
				widget = new CheckBox();
				break;

			case SELECT:
				widget = new Select();
				break;

			case COMBO:
				widget = new ComboBox();
				break;

			case SPREADSHEET:
				widget = new SpreadSheet();
				break;

			case FILE:
				widget = new File();
				break;

			case PASSWORD:
				widget = new Password();
				break;

			case BOOLEAN:
				widget = new BooleanWidget();
				break;

			case NSELECT:
				widget = new NSelect();
				break;

			default:
				widget = new LimsWidget();
				break;
		}
		return widget;
	}

	public static LimsWidget getWidget( String widgetName, AVPair avpair ) {
		if ( widgetName == null || widgetName.length() == 0 ) widgetName = "STRING";

		WidgetType widgetType = WidgetType.valueOf( widgetName );

		if ( avpair.getAttribute().isMultiple() && !avpair.getAttribute().getProtegeValueType().equals(
				ProtegeValueType.Instance ) )
			return new CollectionWidget( avpair, getWidget( widgetType, avpair ) );
		else return getWidget( widgetType, avpair );
	}

	private static LimsWidget getWidget( WidgetType widgetType, AVPair avpair ) {
		LimsWidget widget;

		switch (widgetType) {
			case DATE:
				widget = new Date( avpair );
				break;

			case NUMERIC:
				widget = new Numeric( avpair );
				break;

			case OBJECT_LINK:
				widget = new Object_Link( avpair );
				break;

			case TEXT:
				widget = new Text( avpair );
				break;

			case STRING:
				widget = new LimsWidget( avpair );
				break;

			case RADIO:
				widget = new Radio( avpair );
				break;

			case TEXTAREA:
				widget = new TextArea( avpair );
				break;

			case CHECKBOX:
				widget = new CheckBox( avpair );
				break;

			case SELECT:
				widget = new Select( avpair );
				break;

			case COMBO:
				widget = new ComboBox( avpair );
				break;

			case SPREADSHEET:
				widget = new SpreadSheet( avpair );
				break;

			case FILE:
				widget = new File( avpair );
				break;

			case PASSWORD:
				widget = new Password( avpair );
				break;

			case BOOLEAN:
				widget = new BooleanWidget( avpair );
				break;

			case NSELECT:
				widget = new NSelect( avpair );
				break;

			default:
				widget = new LimsWidget( avpair );
				break;
		}

		System.out.println( "LimsWidget = " + avpair.getAttribute().getName() + " ===> " + widget.getClass() );
		return widget;
	}
}